package com.young.cms.model.gen4;


import com.young.cms.model.enums.EColumnTypeEnum;
import com.young.cms.model.enums.EControlCheckEnum;
import com.young.common.model.enums.EYesOrNoEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * 控件配置
 *
 * @author wangjy
 * @date 2022/03/29 23:57:01
 */
public class ControlConfig {
    /**
     * 数据类型
     * static,url,dict
     */
    private String dataType;
    /**
     * static静态数据选项集合
     */
    private List<OptionItem> optionItemList;
    /**
     * 数据查询地址
     */
    private String dataUrl;
    /**
     * 值字段
     */
    private String keyField;
    /**
     * 文本字段
     */
    private String valueField;
    /**
     * 表单校验
     */
    private List<CheckRuleItem> checkRuleList;

    public static ControlConfig buildDefault(GenColV4 genColV4) {
        ControlConfig controlConfig = new ControlConfig();
        controlConfig.setDataType("static");
        controlConfig.setDataUrl("");
        controlConfig.setKeyField("key");
        controlConfig.setValueField("value");
        controlConfig.setOptionItemList(new ArrayList<>());
        // 校验规则
        controlConfig.setCheckRuleList(new ArrayList<>());
        // 必填则设置必填校验
        if (EYesOrNoEnum.YES.getCode().toString().equals(genColV4.getFieldNotNull())) {
            controlConfig.checkRuleList.add(CheckRuleItem.build(EControlCheckEnum.ErrEmpty, genColV4));
        }
        // 数字则设置数字校验
        if (EColumnTypeEnum.ofOrDefault(genColV4.getFieldType()).isNumberType()) {
            controlConfig.checkRuleList.add(CheckRuleItem.build(EControlCheckEnum.ErrNumber, genColV4));
        }
        // 字符串则校验长度
        if (EColumnTypeEnum.ofOrDefault(genColV4.getFieldType()).isLengthLimitStringType()) {
            controlConfig.checkRuleList.add(CheckRuleItem.build(EControlCheckEnum.ErrLength, genColV4));
        }
        return controlConfig;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getDataUrl() {
        return dataUrl;
    }

    public List<OptionItem> getOptionItemList() {
        return optionItemList;
    }

    public void setOptionItemList(List<OptionItem> optionItemList) {
        this.optionItemList = optionItemList;
    }

    public void setDataUrl(String dataUrl) {
        this.dataUrl = dataUrl;
    }

    public String getKeyField() {
        return keyField;
    }

    public void setKeyField(String keyField) {
        this.keyField = keyField;
    }

    public String getValueField() {
        return valueField;
    }

    public void setValueField(String valueField) {
        this.valueField = valueField;
    }

    public List<CheckRuleItem> getCheckRuleList() {
        return checkRuleList;
    }

    public void setCheckRuleList(List<CheckRuleItem> checkRuleList) {
        this.checkRuleList = checkRuleList;
    }
}
